const electron = require('electron')
const path = require('path')
const {app} = electron
const {BrowserWindow} = electron

let mainWindow
let config = {}

if (process.env.NODE_ENV === 'development') {
    config = require('../config')
    config.url = `http://localhost:${config.port}`
} else {
    config.devtron = false
    config.url = `file://${__dirname}/dist/index.html`
}

function createWindow () {
    mainWindow = new BrowserWindow({width: 800, height: 600})

    mainWindow.loadURL(config.url)

    if (process.env.NODE_ENV === 'development') {
        BrowserWindow.addDevToolsExtension(path.join(__dirname, '../node_modules/devtron'))
        if (process.platform === 'darwin') {
            BrowserWindow.addDevToolsExtension(config.vueDevMacPath)
        } else {
            // BrowserWindow.addDevToolsExtension(config.vueDevWinPath)
        }
        mainWindow.webContents.openDevTools()

        // let installExtension = require('electron-devtools-installer')

        // installExtension.default(installExtension.VUEJS_DEVTOOLS)
        //     .then((name) => mainWindow.webContents.openDevTools())
        //     .catch((err) => console.log('An error occurred: ', err))
    }

    mainWindow.on('closed', () => {
        mainWindow = null
    })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
})
